
Imports cmplib01


Public Class frmSelectSingleUnit

    Const c_MODULE As String = "frmSelectSingleUnit"

    Private ptrGame As cmplib01.CampaignerGameData
    Private ptrdepZone As DeploymentZone
    Private ptrmoveTypeNames() As String
    Private ptrSelectedUnit As UnitType


    Private diagResult As DialogResult = Windows.Forms.DialogResult.OK


    Shadows Function showDialog(ByRef Game As cmplib01.CampaignerGameData, ByRef depZone As DeploymentZone, ByRef movetypenames() As String) As DialogResult

        Try
            ptrGame = Game
            ptrdepZone = depZone
            ptrmoveTypeNames = moveTypeNames

            PopulateList()

            MyBase.ShowDialog()

            Return diagResult

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try

    End Function

    Private Sub PopulateList()
        Try

            Dim i As Integer
            lvwUnits.View = View.SmallIcon
            lvwUnits.SmallImageList = imlIcons

            For Each un As UnitType In ptrGame.UnitTypes

                imlIcons.Images.Add(un.Icon(cmplib01.IconImageIndex_cgd.Owner))

                Dim litem As New ListViewItem(un.TypeName, i)
                litem.Tag = un

                lvwUnits.Items.Add(litem)

                i += 1
            Next

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Sub listUnitValues(ByRef unit As UnitType)
        Try

            lblPointValue.Text = unit.SuggestedPointValue
            lblSpeed.Text = unit.MovementSpeed
            lblMoveType.Text = ptrmoveTypeNames(unit.MovementClassID)


        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub


    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        If lvwUnits.SelectedItems.Count = 0 Then
            diagResult = Windows.Forms.DialogResult.Cancel
        Else
            ptrSelectedUnit = lvwUnits.SelectedItems.Item(0).Tag
            diagResult = Windows.Forms.DialogResult.OK
        End If

        Me.Hide()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        diagResult = Windows.Forms.DialogResult.Cancel
        Me.Hide()
    End Sub

    Private Sub lvwUnits_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvwUnits.ItemSelectionChanged
        Try

            If Not lvwUnits.SelectedItems.Count = 0 Then
                listUnitValues(lvwUnits.SelectedItems.Item(0).Tag)
            Else
                lblPointValue.Text = ""
                lblSpeed.Text = ""
                lblMoveType.Text = ""
            End If

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub
End Class